// pages/login/login.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import {request} from "../../request/request.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: '',
    registerInfo: {
      nickname: '',
      username: '',
      password: '',
      confirmPassword: ''
    },
    loading: false
  },
  inputUsername(event) {
    this.setData({
      username: event.detail
    })
  },
  inputPassword(event) {
    this.setData({
      password: event.detail
    })
  },
  inputRegisterUsername(event) {
    this.setData({
      "registerInfo.username": event.detail
    })
  },
  inputRegisterNickname(event) {
    this.setData({
      "registerInfo.nickname": event.detail
    })
  },
  inputRegisterPassword(event) {
    this.setData({
      "registerInfo.password": event.detail
    })
  },
  inputRegisterComfirmPassword(event) {
    this.setData({
      "registerInfo.confirmPassword": event.detail
    })
  },
  async login() {
    let username = this.data.username;
    let password = this.data.password;
    if(username === '' || password === '') {
      Toast.fail('请输入值！');
      return;
    }
    this.setData({
      loading: true
    })
    const res = await request({url: '/user/userLogin',
     method: 'post', 
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     data: {
      username,
      password
    }});

    console.log(res);
    this.setData({
      loading: false
    })
    if(res.code === 4001) {
      Toast.fail('账号不存在！');
      return;
    }
    if(res.code === 4002) {
      Toast.fail('密码错误！');
      return;
    }
    if(res.code === 2000) {
      this.loginSuccess(res);
    }
  },
  async register() {
    let nickname = this.data.registerInfo.nickname;
    let username = this.data.registerInfo.username;
    let password = this.data.registerInfo.password;
    let confirmPassword = this.data.registerInfo.confirmPassword;
    if(username === '' || password === '' || nickname === '' || confirmPassword === '') {
      Toast.fail('请输入值！');
      return;
    }
    if(nickname.length >= 7) {
      Toast.fail('昵称长度不能超过7！');
      return;
    }
    if(password.length < 6 || confirmPassword.length < 6) {
      Toast.fail('密码长度不能少于6位！');
      return;
    }
    if(password !== confirmPassword) {
      Toast.fail('两次输入的密码不一致！');
      return;
    }

    this.setData({
      loading: true
    })
    const judgeUser = await request({url: '/user/judgeUser',
     method: 'get', 
     data: {
      username
    }});
    if(judgeUser.code === 4000) {
      Toast.fail('改账户已存在！');
      this.setData({
        loading: false
      })
      return;
    }
    const res = await request({url: '/user/insertUser',
     method: 'post', 
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     data: {
      nickname,
      username,
      password,
      'passwordCheck': confirmPassword
    }});
    console.log(res);
    this.setData({
      loading: false
    })
    if(res.code === 4003) {
      Toast.fail('两次输入的密码不一致！');
      return;
    }
    if(res.code === 2000) {
      this.loginSuccess(res);
    }
    console.log(this.data.registerInfo.nickname);
    console.log(this.data.registerInfo.password);
  },
  //登录成功
  loginSuccess(res) {
    //将信息存在全局变量中
    const app = getApp();
    app.globalData.token = res.data.token;
    app.globalData.userInfo.userId = res.data.userId;
    app.globalData.userInfo.username = res.data.username;
    app.globalData.userInfo.nickname = res.data.nickname;
    app.globalData.userInfo.position = res.data.position;
    app.globalData.userInfo.avatar = res.data.avatar;
    //存到本地
    wx.setStorageSync('token', res.data.token);
    wx.setStorageSync('userInfo', app.globalData.userInfo);
    //跳转回我的页面
    wx.switchTab({
      url: '/pages/me/me'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})